package tunnel;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;

/**
 * 小象代理隧道代理示例 - JAVA，类库 Jsoup
 *
 * @author xxdl
 */
public class JsoupDemo {
    // 应用账号（请替换为真实账号）
    private static String appKey = "appKey";
    // 应用密码（请替换为真实密码）
    private static String appSecret = "appSecret";

    private static String proxyHost = "http-dynamic.xiaoxiangdaili.com";
    private static Integer proxyPort = 10030;
    private static String targetUrl = "http://httpbin.org/ip";

    public static void main(String[] args) throws IOException {
        // JDK 8u111版本后，目标页面为HTTPS协议，启用proxy用户密码鉴权
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        Authenticator.setDefault(new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(appKey, appSecret.toCharArray());
            }
        });

        Connection conn = Jsoup.connect(targetUrl)
                .header("Accept-Encoding", "gzip")
                .header("Connection", "close")
                .ignoreContentType(true)
                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));

        System.out.println(conn.get().body().html());

    }
}
